package com.algo.aldi;

import com.alibaba.fastjson.JSONObject;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * 满减券
 */
public class SimpleJob2 implements Job {

    private static Logger logger = LoggerFactory.getLogger(SimpleJob2.class);

    public static void main_1(String[] args) throws SchedulerException, InterruptedException {
        task();
    }

    public static void main(String[] args) throws SchedulerException, InterruptedException {

        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        JobDetail job = newJob(SimpleJob2.class)
                .withIdentity("job1", "group1")
                .build();

        CronTrigger trigger = newTrigger()
                .withIdentity("trigger1", "group1")
                .withSchedule(cronSchedule("0 0 12 * * ? "))
                .build();

        sched.scheduleJob(job, trigger);

        sched.start();
        TimeUnit.HOURS.sleep(24);
    }

    static int TIMES = 2;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            for (int i = 0; i < TIMES; i++)
                task();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //static String couponThemeId = "PMYDqYJiFDrygZq--EcExTR-4sZGM1db_HHVW9Pae-vo5l3elnURGYjhlBC_Y7hNRuNlJEBw51hc_J2wbK8V0uzFAyNSI46-gK7Tw8VxhtnNPnPPAQSaB9og-JsVRImzT5yafqjqPQp12Dlh8SlOaG8VfLNsX69CQ-9hu7HF0FE=";
    static String couponThemeId = "UL8vTKY6s6uItSKMLqsd4KkYAto5bfDKoOOD8jsUAWO5wTjkVuLAOBn0Gr19wvRrou0WQjhnYecwheQVWMTZ4eLCkpAFvsSs-DezeEROEj4vkr3gbFsbPtRA-OykUJdFPDQkB1vCOJ2ghoNOLRY9ysEhLDb5HcqgO5IvPgRD7Qk=";
    static String url = "https://m.aldi.com.cn/api/promotion/coupon/exchangeCoupon?couponThemeId=" + couponThemeId;
    static String ut = "87445417b91f4819966caf15b75f7d7c";
    static RestTemplate restTemplate = new RestTemplate();
    static HttpHeaders headers = new HttpHeaders();
    static HttpEntity<JSONObject> requestEntity = null;

    static {
        headers.add("Accept", "*/*");
        headers.add("Cookie", "locale=zh_CN;ut=" + ut);
        requestEntity = new HttpEntity<JSONObject>(null, headers);
    }
    private static void task() {
        String result = restTemplate.postForObject(url, requestEntity, String.class);
        System.out.println(result);
    }
}
